//
//  DYJReadTableView.m
//  Piece
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "DYJReadTableView.h"

#import "DYJSerialTableViewCell.h"
#import "DYJShortTableViewCell.h"
#import "DYJQuestionTableViewCell.h"
#import "DYJShortViewController.h"


@interface DYJReadTableView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *readTableView;
@property (nonatomic, strong) UIView *floorView;
@end
@implementation DYJReadTableView
- (void)setShortModel:(DYJShortModel *)shortModel
{
    if (_shortModel != shortModel) {
        _shortModel = shortModel;
        [self.readTableView reloadData];
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.floorView = [[UIView alloc] init];
        self.floorView.backgroundColor = [UIColor clearColor];
        self.floorView.layer.masksToBounds = YES;
        self.floorView.layer.cornerRadius = 5;
        self.floorView.userInteractionEnabled = NO;
        //初始化tableView
        self.readTableView = [[UITableView alloc] initWithFrame:self.bounds];
        self.readTableView.showsVerticalScrollIndicator = NO;
        self.readTableView.backgroundColor = [UIColor clearColor];
        self.readTableView.separatorStyle = 0;
        self.readTableView.delegate = self;
        self.readTableView.dataSource = self;
        [self addSubview:_readTableView];
        [self.readTableView addSubview:_floorView];
        _floorView.layer.borderColor = [UIColor colorWithRed:0.86 green:0.87 blue:0.88 alpha:0.8].CGColor;
        _floorView.layer.borderWidth = 2;
        _floorView.layer.masksToBounds = YES;
        //注册cell
        [self.readTableView registerClass:[DYJShortTableViewCell class] forCellReuseIdentifier:@"shortCell"];
        
        [self.readTableView registerClass:[DYJSerialTableViewCell class] forCellReuseIdentifier:@"serialCell"];
        
        [self.readTableView registerClass:[DYJQuestionTableViewCell class] forCellReuseIdentifier:@"questionCell"];
    }
    return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //规定字体的大小样式:
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName,nil];
    //得到新的frame
    CGRect shortRect = [self.shortModel.guide_word boundingRectWithSize:CGSizeMake(ScreenWidth - 90, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGRect serialRect = [self.serialModel.excerpt boundingRectWithSize:CGSizeMake(ScreenWidth - 95, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGRect questionRect = [self.questionModel.answer_title boundingRectWithSize:CGSizeMake(ScreenWidth - 90, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.floorView.frame = CGRectMake(0, 0, self.frame.size.width, shortRect.size.height + serialRect.size.height + questionRect.size.height + 300);
    
    if (indexPath.row == 0) {
        return 100 + shortRect.size.height;
    }
    else if (indexPath.row == 1)
    {
        return 100 + serialRect.size.height + 10;
    }
    else if (indexPath.row == 2)
    {
        return 100 + questionRect.size.height;
    }
    else
    {
        return 100;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        DYJShortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shortCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.shortModel = _shortModel;
        //[self.readTableView insertSubview:_floorView belowSubview:cell];
        return cell;
    }
    else if (indexPath.row == 1)
    {
       DYJSerialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"serialCell"];
        //[self.readTableView insertSubview:_floorView belowSubview:cell];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       cell.serialModel = _serialModel;
       return cell;
    }
    else if (indexPath.row == 2)
    {
        DYJQuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"questionCell"];
        //[self.readTableView insertSubview:_floorView belowSubview:cell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.questionLineView.hidden = YES;
        cell.questionModel = _questionModel;
        return cell;
    }
    else
    {
        DYJQuestionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"questionCell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.questionImageView.hidden = YES;
        cell.questionLineView.hidden = YES;
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self.delegate pushShortViewControllerById:_shortModel.content_id];
    }
    else if (indexPath.row == 1)
    {
        [self.delegate pushSerialViewControllerById:_serialModel.id];
    }
    else if (indexPath.row == 2)
    {
        [self.delegate pushQuestionViewControllerById:_questionModel.question_id];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.readTableView.contentOffset.y < -100) {
        [self.delegate pushSearchViewController];
    };
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
